package com.xiemingtian.navdemo

import android.content.SharedPreferences
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.fragment.NavHostFragment

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val finalHost = NavHostFragment.create(R.navigation.main_nav)
        supportFragmentManager.beginTransaction()
                .replace(R.id.nav_host, finalHost)
                .setPrimaryNavigationFragment(finalHost)
                .commit()
    }

    fun isLogin() : Boolean {
        return getSp().getBoolean("isLogin",false)
    }

    fun getName() : String{
        return getSp().getString("name","")
    }

    fun setIsLogin(isLogin : Boolean,name : String){
        getSp().edit().putBoolean("isLogin",isLogin).putString("name",name).apply()
    }

    private fun getSp() : SharedPreferences {
        val sp = getSharedPreferences("com.xiemingtian.navdemo_sp", MODE_PRIVATE)
        return sp
    }
}
